// 后台用户任务管理API
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const TaskList = require('../models/model/taskList');





/**
 * 后台管理系统查询所有任务
 * @param {第几页} page 
 * @param {页容量} limit 
 * @param {用户ID} userId 
 * @returns 
 */
exports.manageTodayTasks = async (page = 1, limit = 10, userId = "") => {
    const ins = await TaskList.findAll({
        offset: (page - 1) * limit,
        limit: +limit,
        where: {
            userId
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 
 * @param {*} id 任务的id
 * @param {*} isDelete 是否可删除
 */
exports.setManageTodayTasksisDelete = async (id, isDelete) => {
    return await TaskList.update(
        {
            iDelete: isDelete
        },
        {
            where: {
                id
            }
        }
    )
}

/**
 * 
 * @param {任务id} id 
 * @param {是否完成} isDone 
 * @returns 
 */
exports.setManageTodayTasksisDone = async (id, isDone) => {
    return await TaskList.update(
        {
            isDone: isDone
        },
        {
            where: {
                id
            }
        }
    )
}

// 删除今日任务
exports.deleteManageTodayTasks = async (id) => {
    const ins = await TaskList.destroy(
        {
            where: {
                id
            }
        }
    )
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 
 * @param {*} name 任务名
 * @param {*} description 描述
 * @param {*} content 内容
 * @param {*} completeTargetTime 目标完成时间 
 * @param {*} completeTime 完成时间
 * @param {*} isDone 是否完成
 * @param {*} userId 用户id
 */
exports.addManageTodayTasks = async (name = "", description = "", content = "", completeTargetTime = null, completeTime = null, isDone = 0, userId = '') => {
    const ins = await TaskList.create({
        name,
        description,
        content,
        completeTargetTime,
        completeTime,
        isDone,
        userId
    });
    if (ins) {
        return ins.toJSON();
    }
    return null;
}

// 更新任务
exports.updateManageTodayTasks = async (id, name, description, content, completeTargetTime) => {
    return await TaskList.update(
        {
            name,
            description,
            content,
            completeTargetTime
        },
        {
            where: {
                id
            }
        }
    );
}

//-----------------------------------------------------下面需要重构

/**
 * 查询历史任务
 * @param {第几页} page 
 * @param {页容量} limit 
 * @param {模糊查询} search 查询任务名
 * @param {用户id} userId 
 * @returns 
 */
exports.findHistoricalMission = async (page = 1, limit = 10, search = '', userId = "") => {
    let where;
    if (search == "") {
        where = {
            userId,
        }
    } else {
        where = {
            userId,
            name: {
                [Op.like]: '%' + search + '%',
            }
        }
    }
    const ins = await TaskList.findAll({
        offset: (page - 1) * limit,
        limit: +limit,
        order: [
            ['creatiedTime', 'DESC'],
        ],
        where: where,
    })
    if (ins) {
        return ins;
    }
    return null;
}


// 查询总的条数,可search
exports.findHistoricalMissionTotal = async (search, userId) => {
    let where;
    if (search == "") {
        where = {
            userId,
        }
    } else {
        where = {
            userId,
            name: {
                [Op.like]: '%' + search + '%',
            }
        }
    }
    const ins = await TaskList.findAll({
        order: [
            ['creatiedTime', 'DESC'],
        ],
        where: where
    })
    if (ins) {
        return ins;
    }
    return null;
}



/**
 * 删除多条任务
 * @param {*} userId 用户id 
 * @param {*} taskArr 任务id数组
 */
exports.deleteHistoricalMissionArr = async (userId, taskArr) => {
    const ins = await TaskList.destroy({
        where: {
            userId,
            id: {
                [Op.or]: taskArr,
            }
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}




